checkLogin();

var key_text = getCache(login_key);
var xcx_openid = key_text.xcx_openid;
var sid = key_text.sid;
var mid = key_text.mid;

var html='<div class="text ">您暂无二维码，请点击生成</div>';

if(getCache(mid + "_"+sid)) {
    $("#qr_base").attr("src",getCache(mid + "_"+sid).baseImg);
    $('#time').text(getCache(mid + "_"+sid).base_time);
    $('.bill-share-sumbit').text('变更水票共享二维码');
    $('.bill-share-sumbit').attr('data','2');
    $('.bill-share-text').text('变更二维码，之前生成的二维码将失效');
}else {

    //若没有缓存
    var success = function (res) {
        if(res.status == 1){
            var url =  res.data.url;
            setQrcode(url);
        }
        console.log(res)
    }
    var error = function () {
        $('.qr_code_base').append(html);
        $('.bill-share-text').text('');
    }
    getData(share_bill_link,combineParams({type:0}),success,error);

}

function setQrcode(url) {

    var img_len = 0;
    var baseImg = null;
    console.log(url)

    //获取当前时间
    var time = GetUrlString(url,'qrTime');
    $('#time').text(time);
    $('#qr_code').html('');
    $('.bill-share-sumbit').text('变更水票共享二维码');
    $('.bill-share-sumbit').attr('data','2');
    qrCode(url);
    img_len = $("#qr_code").find('img').length;
    if(img_len > 0 ) {
        setTimeout(function(){
            baseImg = $("#qr_code").find('img').attr('src');
            if(baseImg) {
                $("#qr_base").attr("src",baseImg);
                var base_data = {'baseImg':baseImg,'base_time':time};
                setCache(mid + "_"+sid,base_data);
                $('.bill-share-text').text('变更二维码，之前生成的二维码将失效');
            }
        },100)
    }
}


$('.bill-share-sumbit').on('tap',function () {
    $('.text').addClass('none');
    var data = this.getAttribute('data');
    if(isEmpty(data)){
        return;
    }
    var url = null;

    if(data == 1){
        var success = function(res) {
            if(res.status == 1) {
                url = res.data.url;
                setQrcode(url);
            }
        }
        getData(share_bill_link,combineParams({type:1}),success);
    }

    if(data == 2){
        mui.confirm('变更后，以前生成的二维码将失效，确认变更？','提示',['确认','取消'],function (e) {
            if(e.index == 0){
                var success = function(res) {
                    if(res.status == 1) {
                        url = res.data.url;
                        setQrcode(url);
                    }
                }
                getData(share_bill_link,combineParams({type:1}),success);
            }
        })
    }

})


function qrCode(text)
{
    new QRCode(document.getElementById('qr_code'), {width:256,height:256}).makeCode(text);
}

